package com.example.myapplication10;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestQuestionsActivity extends AppCompatActivity {

    private List<Question> questionList;
    private int currentQuestionIndex = 0;
    private Map<Integer, Integer> answers = new HashMap<>();
    private String currentTestName;
    private int[] reverseQuestions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_questions);
        currentTestName = getIntent().getStringExtra("testName");

        // 设置测试标题
        TextView tvTestTitle = findViewById(R.id.tvTestTitle);
        tvTestTitle.setText(currentTestName);

        // 加载测试数据
        loadTestData(currentTestName);
        updateQuestionDisplay();
        Button btnPrev = findViewById(R.id.btnPrev);
        btnPrev.setOnClickListener(v -> {
            if (currentQuestionIndex > 0) {
                saveCurrentAnswer();
                currentQuestionIndex--;
                updateQuestionDisplay();
            } else {
                Toast.makeText(this, "已经是第一题", Toast.LENGTH_SHORT).show();
            }
        });

    }
    private void loadTestData(String testName) {
        switch (testName) {
            case "焦虑自评量表 (SAS)":
                questionList = generateSASQuestions();
                reverseQuestions = new int[]{4, 9, 12, 17, 19};
                break;
            case "抑郁自评量表 (SDS)":
                questionList = generateSDSQuestions();
                reverseQuestions = new int[]{2, 5, 6, 11, 12, 14, 16, 17, 18, 20};
                break;
            case "压力测试":
                questionList = generateStressQuestions();
                reverseQuestions = new int[]{};
                break;
            case "情绪状态测试":
                questionList = generateEmotionQuestions();
                reverseQuestions = new int[]{3, 5};
                break;
            case "人际关系测试":
                questionList = generateRelationshipQuestions();
                reverseQuestions = new int[]{2, 4};
                break;
            default:
                questionList = generateSASQuestions();
                reverseQuestions = new int[]{4, 9, 12, 17, 19};
                currentTestName = "焦虑自评量表 (SAS)";
        }
    }
    private List<Question> generateSASQuestions() {
        List<Question> questions = new ArrayList<>();
        String[] options = {"A. 从没有或很少时间", "B. 小部分时间", "C. 相当多时间", "D. 绝大部分或全部时间"};
        questions.add(new Question("1. 我觉得比平常容易紧张和着急。", options));
        questions.add(new Question("2. 我无缘无故地感到害怕。", options));
        questions.add(new Question("3. 我容易心里烦乱或觉得惊恐。", options));
        questions.add(new Question("4. 我觉得我可能将要发疯。", options));
        questions.add(new Question("5. 我觉得一切都很好，也不会发生什么不幸。", options));
        questions.add(new Question("6. 我手脚发抖打颤。", options));
        questions.add(new Question("7. 我因为头痛、颈痛和背痛而苦恼。", options));
        questions.add(new Question("8. 我感觉容易衰弱和疲乏。", options));
        questions.add(new Question("9. 我觉得心平气和，并且容易安静坐着。", options));
        questions.add(new Question("10. 我觉得心跳得很快。", options));
        questions.add(new Question("11. 我因为一阵阵头晕而苦恼。", options));
        questions.add(new Question("12. 我有晕倒发作，或觉得要晕倒似的。", options));
        questions.add(new Question("13. 我吸气呼气都感到很容易。", options));
        questions.add(new Question("14. 我手脚麻木和刺痛。", options));
        questions.add(new Question("15. 我因为胃痛和消化不良而苦恼。", options));
        questions.add(new Question("16. 我常常要小便。", options));
        questions.add(new Question("17. 我的手常常是干燥温暖的。", options));
        questions.add(new Question("18. 我脸红发热。", options));
        questions.add(new Question("19. 我容易入睡并且一夜睡得很好。", options));
        questions.add(new Question("20. 我做恶梦。", options));
        return questions;
    }
    private List<Question> generateSDSQuestions() {
        List<Question> questions = new ArrayList<>();
        String[] options = {"A. 很少有", "B. 有时有", "C. 大部分时间有", "D. 绝大部分时间有"};
        questions.add(new Question("1. 我感到情绪沮丧，高兴不起来。", options));
        questions.add(new Question("2. 我觉得一天中早晨最好。", options));
        questions.add(new Question("3. 我要哭或想哭。", options));
        questions.add(new Question("4. 我夜间睡眠不好。", options));
        questions.add(new Question("5. 我吃饭像平时一样多。", options));
        questions.add(new Question("6. 我的性功能正常。", options));
        questions.add(new Question("7. 我感到体重减轻。", options));
        questions.add(new Question("8. 我为便秘烦恼。", options));
        questions.add(new Question("9. 我的心跳比平时快。", options));
        questions.add(new Question("10. 我无故感到疲劳。", options));
        questions.add(new Question("11. 我的头脑像往常一样清楚。", options));
        questions.add(new Question("12. 我做事情像平时一样不感到困难。", options));
        questions.add(new Question("13. 我坐卧不安，难以保持平静。", options));
        questions.add(new Question("14. 我对未来感到有希望。", options));
        questions.add(new Question("15. 我比平时更容易激怒。", options));
        questions.add(new Question("16. 我觉得决定什么事很容易。", options));
        questions.add(new Question("17. 我感到自己是有用的和不可缺少的人。", options));
        questions.add(new Question("18. 我的生活很有意义。", options));
        questions.add(new Question("19. 假若我死了别人会过得更好。", options));
        questions.add(new Question("20. 我仍旧喜爱自己平时喜爱的东西。", options));
        return questions;
    }
    private List<Question> generateStressQuestions() {
        List<Question> questions = new ArrayList<>();
        String[] options = {"A. 从不", "B. 偶尔", "C. 经常", "D. 总是"};
        questions.add(new Question("1. 我觉得最近压力很大，难以承受。", options));
        questions.add(new Question("2. 遇到突发状况时，我会不知所措。", options));
        questions.add(new Question("3. 我因为压力经常失眠。", options));
        questions.add(new Question("4. 压力让我对原本喜欢的事失去兴趣。", options));
        questions.add(new Question("5. 我觉得压力导致我注意力难以集中。", options));
        questions.add(new Question("6. 面对压力，我会选择逃避。", options));
        questions.add(new Question("7. 压力让我容易发脾气。", options));
        questions.add(new Question("8. 我觉得压力下身体容易出现不适（如头痛、肌肉紧张等）。", options));
        questions.add(new Question("9. 我有应对压力的有效方法。", options));
        questions.add(new Question("10. 压力让我对未来感到迷茫。", options));
        return questions;
    }
    private List<Question> generateEmotionQuestions() {
        List<Question> questions = new ArrayList<>();
        String[] options = {"A. 完全不符合", "B. 不太符合", "C. 比较符合", "D. 完全符合"};
        questions.add(new Question("1. 我最近心情一直很愉悦。", options));
        questions.add(new Question("2. 我容易陷入焦虑情绪中。", options));
        questions.add(new Question("3. 我会经常感到莫名的悲伤。", options));
        questions.add(new Question("4. 我能很好地控制自己的情绪。", options));
        questions.add(new Question("5. 我觉得情绪低落时很难调整过来。", options));
        questions.add(new Question("6. 我会因为小事就情绪激动。", options));
        questions.add(new Question("7. 我感觉自己情绪很稳定。", options));
        questions.add(new Question("8. 消极情绪会持续很长时间影响我。", options));
        questions.add(new Question("9. 我能快速从不良情绪中走出来。", options));
        questions.add(new Question("10. 我觉得自己情绪变化很频繁。", options));
        return questions;
    }
    private List<Question> generateRelationshipQuestions() {
        List<Question> questions = new ArrayList<>();
        String[] options = {"A. 完全不同意", "B. 不太同意", "C. 比较同意", "D. 完全同意"};
        questions.add(new Question("1. 我很容易和陌生人交流。", options));
        questions.add(new Question("2. 我觉得身边的人很难理解我。", options));
        questions.add(new Question("3. 我有很多聊得来的朋友。", options));
        questions.add(new Question("4. 在团队中我经常感到被孤立。", options));
        questions.add(new Question("5. 我能很好地处理和朋友的矛盾。", options));
        questions.add(new Question("6. 我觉得自己在社交场合很不自在。", options));
        questions.add(new Question("7. 我愿意主动参与社交活动。", options));
        questions.add(new Question("8. 我和家人的关系很紧张。", options));
        questions.add(new Question("9. 我能感受到他人对我的善意。", options));
        questions.add(new Question("10. 我觉得社交是一件很累的事。", options));
        return questions;
    }

    private void updateQuestionDisplay() {
        Question currentQuestion = questionList.get(currentQuestionIndex);
        TextView tvQuestion = findViewById(R.id.tvQuestion);
        tvQuestion.setText(currentQuestion.getQuestionText());

        TextView tvQuestionNumber = findViewById(R.id.tvQuestionNumber);
        tvQuestionNumber.setText("问题 " + (currentQuestionIndex + 1) + "/" + questionList.size());

        RadioGroup rgOptions = findViewById(R.id.rgOptions);
        rgOptions.removeAllViews();

        String[] options = currentQuestion.getOptions();
        for (int i = 0; i < options.length; i++) {
            RadioButton radioButton = new RadioButton(this);
            radioButton.setId(i);
            radioButton.setText(options[i]);
            radioButton.setTextSize(23);
            radioButton.setPadding(0, 15, 0, 15);
            rgOptions.addView(radioButton);
        }

        if (answers.containsKey(currentQuestionIndex)) {
            rgOptions.check(answers.get(currentQuestionIndex));
        }

        // 设置选项改变监听器
        rgOptions.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId != -1) {
                answers.put(currentQuestionIndex, checkedId);
                // 自动跳转下一题或完成测试
                if (currentQuestionIndex < questionList.size() - 1) {
                    currentQuestionIndex++;
                    updateQuestionDisplay();
                } else {
                    calculateResult();
                }
            }
        });
    }

    private void saveCurrentAnswer() {
        RadioGroup rgOptions = findViewById(R.id.rgOptions);
        int selectedId = rgOptions.getCheckedRadioButtonId();
        if (selectedId != -1) {
            answers.put(currentQuestionIndex, selectedId);
        }
    }

    private void calculateResult() {
        if (answers.size() < questionList.size()) {
            Toast.makeText(this, "请回答所有问题", Toast.LENGTH_SHORT).show();
            return;
        }

        int totalScore = 0;
        for (int i = 0; i < questionList.size(); i++) {
            int selectedOption = answers.get(i);
            int score = selectedOption + 1;

            boolean isReverse = false;
            for (int reverseId : reverseQuestions) {
                if (i + 1 == reverseId) {
                    isReverse = true;
                    break;
                }
            }

            if (isReverse) {
                score = 5 - score;
            }

            totalScore += score;
        }

        int standardScore = (int) Math.round(totalScore * 1.25);

        String result = "您的" + currentTestName + "分数为：" + standardScore + "分\n\n";
        if ("焦虑自评量表 (SAS)".equals(currentTestName)) {
            if (standardScore < 50) {
                result += "正常：您的焦虑水平在正常范围内，保持良好的心理状态。";
            } else if (standardScore < 55) {
                result += "轻度焦虑：您可能有轻微的焦虑情绪，建议通过适当的放松方式调整。";
            } else if (standardScore < 60) {
                result += "中度焦虑：您的焦虑情绪较为明显，建议寻求专业心理咨询帮助。";
            } else {
                result += "重度焦虑：您的焦虑情绪较为严重，建议尽快联系专业心理医生进行评估和治疗。";
            }
        } else if ("抑郁自评量表 (SDS)".equals(currentTestName)) {
            if (standardScore < 53) {
                result += "正常：您的抑郁水平在正常范围，继续保持积极心态。";
            } else if (standardScore < 63) {
                result += "轻度抑郁：偶尔情绪低落，可尝试自我调节或与亲友倾诉。";
            } else if (standardScore < 73) {
                result += "中度抑郁：情绪持续低落，建议寻求心理咨询师帮助。";
            } else {
                result += "重度抑郁：情绪问题已严重影响生活，需尽快就医治疗。";
            }
        } else if ("压力测试".equals(currentTestName)) {
            if (standardScore < 30) {
                result += "压力水平低：您目前压力较小，继续保持良好状态。";
            } else if (standardScore < 60) {
                result += "压力水平中等：适当关注压力源，学习压力管理方法。";
            } else {
                result += "压力水平高：压力已对您产生明显影响，建议及时调整或寻求帮助。";
            }
        } else if ("情绪状态测试".equals(currentTestName)) {
            if (standardScore < 30) {
                result += "情绪状态良好：您情绪积极稳定，继续保持。";
            } else if (standardScore < 60) {
                result += "情绪状态一般：存在一些情绪波动，注意自我调节。";
            } else {
                result += "情绪状态较差：情绪问题较突出，建议寻求专业支持。";
            }
        } else if ("人际关系测试".equals(currentTestName)) {
            if (standardScore < 30) {
                result += "人际关系良好：您在社交中较轻松，继续维持。";
            } else if (standardScore < 60) {
                result += "人际关系一般：存在一些社交困扰，可尝试改善沟通方式。";
            } else {
                result += "人际关系较差：社交问题较明显，建议学习社交技巧或寻求帮助。";
            }
        }
        Intent intent = new Intent(this, ResultActivity.class);
        intent.putExtra("result", result);
        startActivity(intent);
        finish();
    }
}